﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace skvote
{
    public partial class admin_question : System.Web.UI.Page
    {
        private OleDbConnection connection = new conn().CreateConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            function.check();
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["act"]) && Request["act"] == "del")
                {
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        OleDbConnection tempconn = new conn().CreateConnection();
                        try
                        {
                            OleDbCommand cmd = new OleDbCommand("select * from xh_question where id=" + Request["id"], tempconn);
                            OleDbDataReader dr = cmd.ExecuteReader();
                            int vcount = 0;
                            string tid = "";
                            if (dr.Read())
                            {
                                vcount = Convert.ToInt32(dr["ps"]);
                                tid = dr["tid"].ToString();
                            }
                            dr.Close();
                            cmd.CommandText = "delete from xh_question where id=" + Request["id"];
                            cmd.ExecuteNonQuery();
                            cmd.CommandText = "update xh_title set vcount=vcount - " + vcount.ToString() + " where id=" + tid;
                            cmd.ExecuteNonQuery();
                        }
                        finally
                        {
                            tempconn.Close();
                        }
                    }
                }
                if (!string.IsNullOrEmpty(Request["tid"]) && !string.IsNullOrEmpty(Request["k"]))
                {
                    BindData("select * from xh_question where tid=" + Request["tid"] + " and question like \"%" + Request["k"].Trim() + "%\" order by id desc");
                }
                else if (!string.IsNullOrEmpty(Request["tid"]) && string.IsNullOrEmpty(Request["k"]))
                {
                    BindData("select * from xh_question where tid=" + Request["tid"] + " order by id desc");
                }
                else if (string.IsNullOrEmpty(Request["tid"]) && !string.IsNullOrEmpty(Request["k"]))
                {
                    BindData("select * from xh_question where question like \"%" + Request["k"] + "%\" order by id desc");
                }
                else
                {
                    BindData("select * from xh_question order by id desc");
                }
            }
        }
        private void BindData(string sql)
        {
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
                DataSet ds = new DataSet();
                da.Fill(ds, "binddata");
                table2.DataSource = ds.Tables[0].DefaultView;
                table2.DataBind();
                ShowStatus(ds.Tables[0].Rows.Count);
            }
            finally
            {
                connection.Close();
            }
        }
        private void ShowStatus(int rows)
        {
            recordlbl.Text = rows.ToString();
            pageslbl.Text = table2.PageCount.ToString();
            pagelbl.Text = (table2.CurrentPageIndex+1).ToString();
        }
        public string GetSubject(string id)
        {
            string Subject = "";
            if (!string.IsNullOrEmpty(id))
            {
                OleDbConnection conn = new conn().CreateConnection();
                try
                {
                    OleDbCommand cmd = new OleDbCommand("select * from xh_question where id=" + id.Trim(), conn);
                    OleDbDataReader dr = cmd.ExecuteReader();
                    string sid = "";
                    if (dr.Read())
                    {
                        sid = dr["sid"].ToString();
                    }
                    dr.Close();
                    cmd.CommandText = "select subject from xh_subject where id=" + sid;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        Subject = dr["subject"].ToString();
                    }
                    dr.Close();
                    cmd.Dispose();
                }
                finally
                {
                    conn.Close();
                }
            }
            return Subject;
        }
        public string GetTitle(string id)
        {
            string title = "";
            if (!string.IsNullOrEmpty(id))
            {
                OleDbConnection conn = new conn().CreateConnection();
                try
                {
                    OleDbCommand cmd = new OleDbCommand("select * from xh_question where id=" + id.Trim(), conn);
                    OleDbDataReader dr = cmd.ExecuteReader();
                    string tid = "";
                    if (dr.Read())
                    {
                        tid = dr["tid"].ToString();
                    }
                    dr.Close();
                    cmd.CommandText = "select id,title from xh_title where id=" + tid;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        title = "<a href='admin_question.aspx?tid=" + dr["id"].ToString() + "'>" + dr["title"].ToString() + "</a>";
                    }
                    dr.Close();
                    cmd.Dispose();
                }
                finally
                {
                    conn.Close();
                }
            }
            return title;
        }

        protected void table2_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header)
            {
                CheckBox headerchk = (CheckBox)e.Item.FindControl("headerchk");
                headerchk.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                CheckBox footerchk = (CheckBox)e.Item.FindControl("footerchk");
                footerchk.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
            }
            else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataGridItem item = e.Item;
                item.Attributes.Add("Id", "tabletitle2");
                item.Attributes.Add("className", "out_TableTitle3");
                item.Attributes.Add("onmouseover", "this.className='over_TableTitle3'");
                item.Attributes.Add("onmouseout", "this.className='out_TableTitle3'");
            }
        }
        private void checkbox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkbox = (CheckBox)sender;
            foreach (DataGridItem item in table2.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("choosechk");
                chk.Checked = chkbox.Checked;
            }
        }

        protected void del_Click(object sender, EventArgs e)
        {
            string ids = "";
            foreach (DataGridItem item in table2.Items)
            {
                CheckBox chkbox = (CheckBox)item.FindControl("choosechk");
                if (chkbox.Checked)
                {
                    ids += chkbox.ToolTip.Trim()+",";
                }
            }
            if (string.IsNullOrEmpty(ids))
            {
                function.showErr("没有选中要删除的答案", Response, Request);
            }
            ids = ids.Substring(0, ids.Length - 1);
            OleDbConnection tempconn = new conn().CreateConnection();
            try
            {
                OleDbCommand cmd = new OleDbCommand("delete from xh_question where id in (" + ids.Trim() + ")", tempconn);
                cmd.ExecuteNonQuery();
            }
            finally
            {
                tempconn.Close();
            }
            function.successMsg("记录已成功删除");
        }

        protected void search_Click(object sender, EventArgs e)
        {
            Response.Redirect("admin_question.aspx?tid=" + Request["tid"] + "&k=" + Request["k"]);
        }

        protected void first_Click(object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            switch (btn.CommandArgument)
            {
                case "FIRST": table2.CurrentPageIndex = 0; break;
                case "PREV": if (table2.CurrentPageIndex > 0)
                    {
                        table2.CurrentPageIndex--;
                    }
                    break;
                case "NEXT": if (table2.CurrentPageIndex < (table2.PageCount-1))
                    {
                        table2.CurrentPageIndex++; 
                    }
                    break; 
                case "LAST": table2.CurrentPageIndex = table2.PageCount - 1; break;
            }
            if (!string.IsNullOrEmpty(Request["tid"]) && !string.IsNullOrEmpty(Request["k"]))
            {
                BindData("select * from xh_question where tid=" + Request["tid"] + " and question like \"%" + Request["k"].Trim() + "%\" order by id desc");
            }
            else if (!string.IsNullOrEmpty(Request["tid"]) && string.IsNullOrEmpty(Request["k"]))
            {
                BindData("select * from xh_question where tid=" + Request["tid"] + " order by id desc");
            }
            else if (string.IsNullOrEmpty(Request["tid"]) && !string.IsNullOrEmpty(Request["k"]))
            {
                BindData("select * from xh_question where question like \"%" + Request["k"] + "%\" order by id desc");
            }
            else
            {
                BindData("select * from xh_question order by id desc");
            }
        }

        protected void table2_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            table2.CurrentPageIndex = e.NewPageIndex;
            if (!string.IsNullOrEmpty(Request["tid"]) && !string.IsNullOrEmpty(Request["k"]))
            {
                BindData("select * from xh_question where tid=" + Request["tid"] + " and question like \"%" + Request["k"].Trim() + "%\" order by id desc");
            }
            else if (!string.IsNullOrEmpty(Request["tid"]) && string.IsNullOrEmpty(Request["k"]))
            {
                BindData("select * from xh_question where tid=" + Request["tid"] + " order by id desc");
            }
            else if (string.IsNullOrEmpty(Request["tid"]) && !string.IsNullOrEmpty(Request["k"]))
            {
                BindData("select * from xh_question where question like \"%" + Request["k"] + "%\" order by id desc");
            }
            else
            {
                BindData("select * from xh_question order by id desc");
            }
        }
    }
}
